Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: cancel all outgoing requests when disconnect to prevent ANR #501

Merged
merged 17 commits into from
Feb 22, 2024

Conversation

jyyi1
Copy link
Contributor

@jyyi1 jyyi1 commented Feb 6, 2024

In this PR, I fixed an ANR issue by adding a context.Context object to the doh.Transport.Query method. This ctx will be passed to all network related calls, and it will be cancelled when user Disconnects.

But if we just simply add ctx to the method, gomobile requires we export all related packages (e.g., time) that are used by context.Context, which is not applicable. Therefore we refactored the go code structure and introduced a new backend package that will be the only interface that Java code can use.

In addition, I retired github.com/eycorsican/go-tun2socks/common/log and introduced our own logging package.

TODO Items

The refactoring of backend package is not done yet (for example, intra, intra/split and intra/protect are still exported to Java), but it will involve a lot of changes that are not related to this PR, therefore I would postpone them to future changes.

@jyyi1 jyyi1 requested a review from fortuna February 21, 2024 02:32
@jyyi1 jyyi1 marked this pull request as ready for review February 21, 2024 02:33
}
return v.(Transport)
}
var Debug = log.New(io.Discard, "[DEBUG] ", log.LstdFlags)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not an efficient way to do logging, because you are still generating the messages, and discarding them. And you can't check the log levels.

Let's use slog instead:
https://pkg.go.dev/log/slog
https://go.dev/blog/slog

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated to slog

Android/app/src/go/logging/logging.go Outdated Show resolved Hide resolved
Android/app/src/go/doh/doh.go Outdated Show resolved Hide resolved
Android/app/src/go/doh/doh.go Outdated Show resolved Hide resolved
Android/app/src/go/doh/doh.go Outdated Show resolved Hide resolved
Android/app/src/go/backend/doh.go Outdated Show resolved Hide resolved
Android/app/src/go/backend/doh.go Outdated Show resolved Hide resolved
Comment on lines 28 to 30
type DoHServer struct {
tspt doh.Transport
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
type DoHServer struct {
tspt doh.Transport
}
type DoHServer struct {
server doh.Server
}

Copy link
Contributor Author

@jyyi1 jyyi1 Feb 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

renamed to type DoHServer struct { r doh.Resolver }

*intra.Tunnel
}

func (s *Session) SetDoHServer(svr *DoHServer) { s.SetDNS(svr.tspt) }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We pass the server on ConnectSession. Is this ever used?

Copy link
Contributor Author

@jyyi1 jyyi1 Feb 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, user can update the DoH server even during connected session.

Android/app/src/main/java/app/intra/net/go/GoProber.java Outdated Show resolved Hide resolved
@jyyi1 jyyi1 requested a review from fortuna February 21, 2024 20:34
Android/app/src/go/logging/logging.go Outdated Show resolved Hide resolved
@jyyi1 jyyi1 merged commit 4240f84 into master Feb 22, 2024
5 checks passed
@jyyi1 jyyi1 deleted the junyi/fix-disconnect-anr branch February 22, 2024 23:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants